package wili.sql;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Map;

import wili.c.Ca;

/**
 * @author weila 2022年7月23日
 */
public class SqlCont extends SqlCont0 {
	DatabaseMetaData dbMeta;

	@Override
	public DatabaseMetaData getDatabaseMetaData(String nam) {
		if (dbMeta != null)
			return dbMeta;
		try {
			dbMeta = this.getConn().getMetaData();
			return dbMeta;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public Tab getTable(String nam) {
		Tab ret = tableMap.get(nam);
		if (ret == null) {
			tableMap.put(nam, ret = new Tab(nam, this));
		}
		return ret;
	}

	public SqlCont(String kind, String name) {
		super(kind, name);
	}

	public static void readAll_tableContent_test() {
		Connection conn = Conn9a.mysql("mysql");
		Coms.test_readAll_tableContent_to_textFile(conn);
	}

	public static void printTableInfos_test() {
		SqlCont0 cont = new SqlCont("hana", "");
		Map<String, Tab> map = cont.findTableMap(".+");
		for (String nam : map.keySet()) {
			Tab tab = map.get(nam);
			if (tab == null) {
				Coms.log("printTableInfos_test:" + nam + " is null");
				continue;
			}
			tab.print();
		}
		Ca.pause();
	}

	public static void main(String[] args) throws SQLException {
		Connection conn = null;
		try {
			int opt = 1;
			if (opt == 1) {
				printTableInfos_test();
			} else if (opt == 2) {
				readAll_tableContent_test();
			} else {
			}
		} finally {
			if (conn != null)
				conn.close();
		}
	}

	@Override
	public Map<String, Tab> getTables(String nameReg) {
		return null;
	}
}
